package com.joysuch.wwyt.bp.repository;

import com.joysuch.wwyt.bp.bean.inspect.BPInspectPointDetailBean;
import com.joysuch.wwyt.bp.entity.inspect.BPInspectPointDetail;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface BPInspectPointDetailRepository extends JpaRepository<BPInspectPointDetail, Long> {
    @Query(value = "select  id  from BPInspectPointDetail  where inspectPointId=?1  order by id " )
    List<Long> findExistingIds(Long inspectPointId);

    List<BPInspectPointDetail> findAllByInspectPointId(Long inspectPointId);

//    @Query(value = "SELECT d.id, d.`inspect_item`, d.`inspect_point_id`, d.`inspect_standard`, " +
//            " c.`DEVICE_TYPE_NAME`, c.`NAME` FROM  wwyt_bp_inspect_point_detail  d , " +
//            " wwyt_bp_device_facility_collection c  WHERE d.`device_id` = c.`ID` ", nativeQuery = true)

    @Query(value ="select new com.joysuch.wwyt.bp.bean.inspect.BPInspectPointDetailBean(d.id as id , d.inspectItem as inspectItem, " +
            " d.inspectPointId as inspectPointId, d.inspectStandard as inspectStandard, c.code as deviceBrand, " +
            "  c.name as deviceName, c.id as deviceId, d.inspectItemIds as inspectItemIds ) from BPInspectPointDetail d inner join BpDeviceFacilityCollection c " +
            " on d.deviceId = c.id and d.inspectPointId=:id ")
    List<BPInspectPointDetailBean> findDetails(@Param("id") Long inspectPointId);


    void deleteBPInspectPointDetailByInspectPointId(Long inspectPointId);

    @Query("select c.name from BPInspectPointDetail d inner join BpDeviceFacilityCollection c on d.deviceId = c.id where d.id=:id")
    String findDeviceNameByDetailId(Long id);
}
